import { getCookies } from "utils/cookies";

export default async function({ app, isServer, store, error, query }) {
  let sid = "";
  // 首先从url上获取
  if (query.sid) {
    sid = query.sid;
  } else if (getCookies(sid)) {
    sid = getCookies(sid);
  }
  if (!sid) {
    return;
  }
  // 去加载一下用户的信息
  let userInfo = await app.$axios.get(
    `/yjz-user/lawtrain/pcUserInfo/selectUserInfoByUserId.do`,
    {
      headers: {
        sid: sid
      }
    }
  );
  // 如果sid获取用户信息失败
  if (!userInfo || userInfo.respCode !== "0") {
    store.commit("account/s_login", 0);
    return;
  }
  store.commit("account/s_login", 1);
  store.commit("user/s_user", userInfo.result);
  let userVipInfo = await app.$axios.get(`/lt-order/quert_user_is_vip`, {
    headers: {
      sid: sid
    }
  });
  // 如果sid获取用户信息失败
  if (!userVipInfo || userVipInfo.respCode !== "0") {
    return;
  }

  store.commit("user/s_vip", userVipInfo.result);
}
